<%@ page import="ro.gateway.aida.usr.*,
                 ro.gateway.aida.db.PersistenceToken,
                 ro.gateway.aida.utils.Utils,
                 ro.gateway.aida.db.PersistenceManager,
                 ro.xblue.translator.ShowEditButtonAction,
                 ro.gateway.aida.obj.ActivityStatus,
                 ro.gateway.aida.obj.db.ActivityStatusDB,
                 ro.xblue.translator.LanguageBean,
                 ro.xblue.translator.TranslatorUtils,
                 ro.gateway.aida.utils.HttpUtils,
			  ro.gateway.aida.obj.db.SystemPropertyDB,
			  ro.gateway.aida.autodates.StatusAutomation$Task"%>
<%@ taglib uri="/WEB-INF/translator.tld" prefix="trans" %>
<%@ taglib uri="/WEB-INF/usr.tld" prefix="usr" %>
<%@ taglib uri="/WEB-INF/taglib.tld" prefix="lpd" %>
<%
	PersistenceToken token = PersistenceManager.tokenLookup( application );
	String ctx = request.getContextPath();
	User logged_user = UserUtils.getLogged(request);
	Utils.setBreadCrumbs(request,"bc.admin");
	if (logged_user == null || (!Arbiter.getManager(token ).canDo(request, Perms.ADMIN_TOOLS))) {
		request.getRequestDispatcher("/frw.do?fw=norights").
		forward(request, response);
		return;
	}

	ActivityStatus[] st_statuses = ActivityStatusDB
		.getManager(token ).getHarmonized();
	if (st_statuses == null) st_statuses = new ActivityStatus[0];
	LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, application );

	String event = HttpUtils.getValidTrimedString(request, "event", "updatestatus");
	if ("POST".equalsIgnoreCase(request.getMethod())) {
		if (event.equals("updatestatus")) {
			for ( int i = 0; i < st_statuses.length; i++ ) {
				String new_value = HttpUtils.getValidTrimedString(
				request, "st_" + st_statuses[i].getId(), null);
				if (new_value != null) {
					st_statuses[i].setStatus(lang_bean.language, new_value);
					try {
						ActivityStatusDB.getManager(token ).update(st_statuses[i]);
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		} else if (event.equals("updateautomation")) {
               String automation = HttpUtils.getValidTrimedString(request, "automation", "false").toLowerCase();
               SystemPropertyDB.getManager(token).setProperty("statusautomation", automation);
		}
	}

	String s_automation_on = SystemPropertyDB.getManager(token).getProperty("statusautomation");
	boolean automation_on = s_automation_on != null && s_automation_on.equals("true");
	StatusAutomation$Task statusautomation_task = (StatusAutomation$Task) application
		  .getAttribute(StatusAutomation$Task.TASK_BEAN_NAME);
	boolean status_task_working = statusautomation_task != null && statusautomation_task.isRunning();
%>
<html>

<head>
    <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
     <META HTTP-EQUIV="EXPIRES" CONTENT="0">
    <title>Status editor</title>
<link rel="stylesheet" href="alf.css" type="text/css">
</head>

<body>
<jsp:include page="header.jsp" />

<table align="center" width="500" border="0" cellpadding="5" cellspacing="1" background=images/pixelbgtable.gif class="text_search">
      <tr>
            <td background=images/pixelbgtable.gif class="textalb">
            Activity status editor
            </td>
      </tr>
      <form method="post" action="status_editor.jsp">
	 <input type="hidden" name="event" value="updatestatus"/>
      <tr>
            <td colspan="2" bgcolor="#FFFFFF">
            <p>
            <b><u>Harmonized statuses:</u></b>
            </p>
            <% for (int i = 0; i < st_statuses.length; i++) { %>
            <p style="margin:3px;">
            Code:<%=st_statuses[i].getSt_code()%><br>
            <input type="text" name="st_<%=st_statuses[i].getId()%>"
                  value="<%=st_statuses[i].getStatus(lang_bean.language)%>" />
            </p>
            <% } %>

            <br/>
            <input type="button" name="ignored1" value="Update" class="butonsave"
                  onClick="this.form.submit();" />
            </td>
      </tr>
      </form>
      <form method="post" action="status_editor.jsp">
	 <input type="hidden" name="event" value="updateautomation"/>
	 <input type="hidden" name="automation" value=""/>
      <tr>
		<td colspan="2" bgcolor="#FFFFFF">
		<p><b><u>Status automation:</u></b></p>
		<p>
		Rules for Dates Automation:<br/>
		&middot;The project will start out in Planned Status until the Acutal Approved date is reached at which point the status will be changed to Committed.<br/>
		&middot;The Committed status will remain until the Actual Start Date is reached at which point the status will change to Ongoing.<br/>
		&middot;The Ongoing status will remain until the Actual Closed Date is reached at which point the status will change to Complete.<br/>
		</p>
		<% if (status_task_working) { %>
		... Status automation task is working right now ...
		<br/>
		... Please try again later ...
		<% } else { %>
			<% if (automation_on) { %>
			<input type="button" name="ignored1" value="Deactivate status automation" class="butonsave"
			onClick="this.form.automation.value='false';this.form.submit();" />
			<% } else { %>
			<input type="button" name="ignored1" value="Activate status automation" class="butonsave"
			onClick="this.form.automation.value='true';this.form.submit();" />
			<% } %>
		<% } %>
            </td>
      </tr>
      <tr>
		<td colspan="2" bgcolor="#FFFFFF">
            <input type="button" name="ignored" value="Back to admin page" class="butonsave"
                  onClick="javascript:window.location='admin.jsp';" />
            </td>
      </tr>
      </form>
</table>
      <jsp:include page="footer.jsp" />
</body>
 <HEAD>
 <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
 </HEAD>